/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.model.questionnaire;

import com.blandware.atleap.model.core.BaseObject;

import java.util.List;
import java.util.ArrayList;

/**
 * <p>Represents an answer to a questionnaire question.</p>
 * <p><a href="Answer.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.2 $ $Date: 2006/06/15 10:52:36 $
 * @hibernate.class table="`al_questionnaire_answer`" lazy="false"
 */
public class Answer extends BaseObject {

    /**
     * ID
     */
    protected Long id;

    /**
     * Filled questionnaire in which answer was given
     */
    protected FilledQuestionnaire filledQuestionnaire;

    /**
     * Question to which answer is given
     */
    protected Question question;

    /**
     * Textual answer
     */
    protected String text;

    /**
     * List of selected answer options
     */
    protected List answerOptions = new ArrayList();

    /**
     * Returns ID.
     *
     * @return ID
     * @struts.form-field
	 * @hibernate.id column="`id`"
	 * generator-class="increment" unsaved-value="null"
     */
    public Long getId() {
        return id;
    }

    /**
     * Sets ID.
     *
     * @param id ID to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * Returns a filled questionnaire in which answer was given.
     *
     * @return filled questionnaire
     * @struts.form-field
	 * @hibernate.many-to-one column="`f_questionnaire_id`" not-null="true"
     * outer-join="false" lazy="false"
     */
    public FilledQuestionnaire getFilledQuestionnaire() {
        return filledQuestionnaire;
    }

    /**
     * Sets a filled questionnaire in which answer was given.
     *
     * @param filledQuestionnaire filled questionnaire to set
     */
    public void setFilledQuestionnaire(FilledQuestionnaire filledQuestionnaire) {
        this.filledQuestionnaire = filledQuestionnaire;
    }

    /**
     * Returns question.
     *
     * @return question
     * @struts.form-field
	 * @hibernate.many-to-one column="`question_id`" not-null="true"
     * outer-join="false" lazy="false"
     */
    public Question getQuestion() {
        return question;
    }

    /**
     * Sets question.
     *
     * @param question question to set
     */
    public void setQuestion(Question question) {
        this.question = question;
    }

    /**
     * Returns answer text.
     *
     * @return answer text
     * @struts.form-field
	 * @hibernate.property
	 * @hibernate.column name="`text`" not-null="false" unique="false"
     * length="252"
     */
    public String getText() {
        return text;
    }

    /**
     * Sets answer text.
     *
     * @param text text to set
     */
    public void setText(String text) {
        this.text = text;
    }

    /**
     * Returns list of selected answer options.
     *
     * @return list of answer options
     */
    public List getAnswerOptions() {
        return answerOptions;
    }

    /**
     * Adds a connection between this answerOption and a given option.
     *
     * @param answerOption the answer option to be connected with
     */
    public void addAnswerOption(AnswerOption answerOption) {
        if ( !answerOption.getAnswers().contains(this) ) {
            answerOption.getAnswers().add(this);
        }
        if ( !getAnswerOptions().contains(answerOption) ) {
            getAnswerOptions().add(answerOption);
        }
    }

    /**
     * Removes a connection between this answerOption and a given option.
     *
     * @param answerOption the answer option
     */
    public void removeAnswerOption(AnswerOption answerOption) {
        answerOption.getAnswers().remove(this);
        getAnswerOptions().remove(answerOption);
    }

    /**
     * Sets list of selected answer options.
     *
     * @param answerOptions list to set
     * @hibernate.bag table="`al_questionnaire_option_answer`"
     * cascade="none" lazy="true" inverse="true" outer-join="false"
     * @hibernate.collection-key
     * @hibernate.collection-key-column name="`answer_id`"
     * @hibernate.collection-many-to-many class="com.blandware.atleap.model.questionnaire.AnswerOption"
     * column="`option_id`" outer-join="false"
     */
    public void setAnswerOptions(List answerOptions) {
        this.answerOptions = answerOptions;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Answer)) {
            return false;
        }

        final Answer answer = (Answer) o;

        if (filledQuestionnaire != null
                ? !filledQuestionnaire.equals(answer.filledQuestionnaire)
                : answer.filledQuestionnaire != null) {
            return false;
        }
        if (question != null
                ? !question.equals(answer.question) : answer.question != null) {
            return false;
        }

        return true;
    }

    public int hashCode() {
        int result;
        result
                = (filledQuestionnaire != null
                ? filledQuestionnaire.hashCode() : 0);
        result = 29 * result + (question != null ? question.hashCode() : 0);
        return result;
    }
}
